package com.wty.app.bthcar.adapter;

import static com.lxj.xpopup.enums.PopupAnimation.ScrollAlphaFromTop;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gcssloop.view.utils.DensityUtils;
import com.github.gzuliyujiang.wheelpicker.contract.OnNumberSelectedListener;
import com.github.gzuliyujiang.wheelpicker.widget.NumberWheelLayout;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.animator.ScrollScaleAnimator;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.CenterPopupView;
import com.wty.app.bthcar.R;
import com.wty.app.bthcar.bluetooth.DeviceListActivity;
import com.wty.app.bthcar.info.DeviceBean;
import com.wty.app.bthcar.utils.Constant;
import com.wty.app.bthcar.utils.PreferenceUtil;

import java.util.ArrayList;
import java.util.Set;

public class SetRateDialog extends CenterPopupView {

    private OnItemChangeListener itemListener;
    private NumberWheelLayout rateView;
    private int value;

    public SetRateDialog(@NonNull Context context,OnItemChangeListener changeListener) {
        super(context);
        this.itemListener = changeListener;
    }

    public interface OnItemChangeListener{
        void OnResult(int what);
    }

   /* @Override
    protected int getInnerLayoutId() {
        return R.layout.set_speed_rate_layout;
    }*/

    @Override
    protected int getImplLayoutId() {
        return R.layout.set_speed_rate_layout;
    }

    @Override
    protected int getMaxWidth() {
        return DensityUtils.dip2px(getContext(),300);
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        rateView = findViewById(R.id.rate_wheel);
        rateView.setRange(0,999,1);
        findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        int speedRate = PreferenceUtil.getInstance().getSpeedRate();
        rateView.setOnNumberSelectedListener(new OnNumberSelectedListener() {
            @Override
            public void onNumberSelected(int position, Number item) {
                value = item.intValue();
                PreferenceUtil.getInstance().writePreferences(Constant.DEFAULT_SPEED_KEY, value);
                if (itemListener!=null){
                    itemListener.OnResult(value);
                }
            }
        });
        rateView.setDefaultValue(speedRate);

    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        if (value>0)
            Toast.makeText(getContext(),"速率范围已更新为:0-"+value,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected PopupAnimator getPopupAnimator() {
        return new ScrollScaleAnimator(getPopupContentView(), getAnimationDuration(), ScrollAlphaFromTop);
    }
}
